package com.masterclient.app.businessmodule.minemodule.message

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.eoner.common.config.Config
import com.masterclient.app.R
import com.masterclient.app.businessmodule.minemodule.message.adapter.CouponMsgAdapter
import com.eoner.baselibrary.bean.personal.CouponMsgBean
import com.masterclient.app.businessmodule.minemodule.message.contract.CounponMsgContract
import com.masterclient.app.businessmodule.minemodule.message.contract.CounponMsgPresenter
import com.eoner.baselib.utils.sp.SPUtils
import com.eoner.baselib.widget.navgation.FCNavigationBar
import com.eoner.common.config.BusinessConfig
import com.masterclient.app.base.FCBusinessActivity
import kotlinx.android.synthetic.main.activity_coupon_msg.*

/**
 * 作者：sean
 * 创建时间：2018/5/22 18:47
 * 功能说明：
 */
class CouponMsgActivity : FCBusinessActivity<CounponMsgPresenter>(), CounponMsgContract.View {

    var couponAdapter = CouponMsgAdapter()
    var page = 1
    val spUtils = SPUtils.getInstance()

    override fun useRefreshLayout(): Boolean {
        return true
    }

    override fun requireGetLayoutId(): Int {
        return R.layout.activity_coupon_msg;
    }

    override fun requireSetCustomNavigationBar(navigationBar: FCNavigationBar?) {
        navigationBar?.setTitle("优惠券消息")
    }

    override fun requireInitUIAndData(savedInstanceState: Bundle?) {
        recyclerView.layoutManager = LinearLayoutManager(mContext)
        recyclerView.adapter = couponAdapter

        couponAdapter.bindToRecyclerView(recyclerView)
        couponAdapter.emptyView = View.inflate(mContext, R.layout.null_card, null)
        val titleView = couponAdapter.emptyView.findViewById(R.id.tv_tip) as TextView
        titleView.text = "暂时还没有优惠券快到期哦"
        val tv_msg = couponAdapter.emptyView.findViewById(R.id.tv_desc) as TextView
        tv_msg.text = "每个认真生活的人都值得被认真对待~"
//        couponAdapter.setFooterView(View.inflate(mContext,R.layout.view_foot4,null))
        mPresenter.getSalesruleMessage(page.toString(), BusinessConfig.PAGE_SIZE, false)

        baseRefreshLayout().setOnRefreshListener {
            page = 1
            mPresenter.list.clear()
            couponAdapter.setEnableLoadMore(true)
            mPresenter.getSalesruleMessage(page.toString(), BusinessConfig.PAGE_SIZE, true)
        }

        couponAdapter.setOnLoadMoreListener(BaseQuickAdapter.RequestLoadMoreListener {
            page++
            mPresenter.getSalesruleMessage(page.toString(), BusinessConfig.PAGE_SIZE, false)
        }, recyclerView)
    }


    override fun showFailed() {
        if (baseRefreshLayout().isRefreshing) {
            baseRefreshLayout().isRefreshing = false
        }
        if (page > 1) {
            couponAdapter.loadMoreFail();
        } else {
            couponAdapter.setEnableLoadMore(false)
        }

    }

    override fun onClickPageStateErrorView() {
        super.onClickPageStateErrorView()
        page = 1
        mPresenter.list.clear()
        couponAdapter.setEnableLoadMore(true)
        mPresenter.getSalesruleMessage(page.toString(), BusinessConfig.PAGE_SIZE, false)
    }

    override fun showData(dataBean: CouponMsgBean.DataBean, page: String) {
        if (baseRefreshLayout().isRefreshing) {
            baseRefreshLayout().isRefreshing = false
        }
        if (dataBean.sh_items != null && dataBean.sh_items.size > 0) {
            if (page == "1") {
                spUtils.put(Config.COUPON_MSG, dataBean.sh_items[0].sh_id)
            }
            mPresenter.list.addAll(dataBean.sh_items)
            couponAdapter.setNewData(mPresenter.list)
            couponAdapter.setEnableLoadMore(true)
        } else {
            couponAdapter.setEnableLoadMore(false)
        }

//        if ((dataBean.sh_items == null || dataBean.sh_items.size<=0 )
//                &&"1".equals(page)){
//              VISIBLE(couponAdapter.emptyView)
//        }else {
//            GONE(couponAdapter.emptyView)
//        }
    }

}